home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PsL Monthly 1993 December
/
PSL Monthly Shareware CD-ROM (December 1993).iso
/
prgmming
/
dos
/
pascal
/
tp_asm.exe
/
lha
/
TP&ASM.OOP
< prev
next >
Wrap
Text File
|
1989-07-31
|
23KB
|
648 lines
TP&Asm Integrated Compile-Time Assembler Version 2.2
Copyright (c) 1989 Richard W. Prescott
All Rights Reserved
═══════ Built-In Assembly Language Support for Turbo Pascal Compilers ═══════
All brand and product names mentioned herein are trademarks or registered
trademarks of their respective holders.
┌─────────────────────────────────────────────────────────────────────┐
│ This file contains detailed reference information to enable you to │
│ make effective use of the TP&Asm assembly environment. For general │
│ information on getting started please see the README file. │
└─────────────────────────────────────────────────────────────────────┘
This file contains information specific to the use of TP&Asm with
Turbo Pascal 5.5 Objects and Methods. It is intended to supplement
the information contained in the files TP&ASM.REF and TP&ASM2.REF.
Turbo Pascal Version 5.5 extends Turbo Pascal through the addition
of the Object Oriented Programming (OOP) concepts of "Objects" and
"Methods". Briefly, an "Object" is a structured type which extends
the concept of a Record, while a "Method" is a procedure or function
which is associated with an Object in somewhat the same way as data
components are associated with Records. It is assumed that the reader
is familiar with these concepts and has read Chapter 1 of the Version
5.5 OOP Guide.
Please keep in mind the following distinctions:
Objects can be STATIC or DYNAMIC;
according to whether they are statically defined in a VAR
or CONST statement, or dynamically allocated on the Heap;
Methods can be STATIC or VIRTUAL;
according to whether they are defined "virtual", and
thereby given an entry in a special structure called
the Virtual Method Table (VMT);
Method CALLs can be DIRECT or VIRTUAL;
according to whether the target address of the call is
"bound" into a direct call at compile time, or obtained
from a lookup in the Virtual Method Table at run time.
A Virtual method call is actually a standard Far Indirect Call in
which the DWORD (Pointer) address of the method entry code is found
in the Virtual Method Table. It is also possible to make indirect
calls via a standard Pascal Pointer or assembly DWord variable, and
the address to be loaded into the pointer can either be bound at
compile time or obtained from the VMT mechanism.
Note that in both Pascal and assembly language it is possible to
make DIRECT calls to VIRTUAL methods.
The following topics are described below:
1. Using Assembly Language in Methods
2. Accessing the Self parameter
3. Accessing Object data fields
4. Calling an Object's Methods
5. TypeOf and SizeOf for an Instance
1. Using Assembly Language in Methods
Version 5.5 permits Methods to be written either entirely in Pascal
or entirely in (External) Assembly language. TP&Asm provides the
option of writing any portion of a Method in assembly language,
using the TP&Asm "Assemble" statement. (A method, procedure, or
function can have any number of Assemble statements).
TP&Asm also provides the "Internal" statement, which can be used to
write an entire method in assembly using standard External Assembly
language format. This section provides examples using the Assemble
and the Internal statements to implement an entire method in
assembly language.
As a simple example, consider the implementation of an "Area" method
for the Rect object defined on page 103 of the OOP Guide. The Rect
data fields are defined to be:
X1, Y1, X2, Y2: Integer;
where (X1,Y1) is the upper left corner and (X2,Y2) is the lower right
corner. The Pascal implementation of this method would simply be
Function Rect.Area: Integer;
BEGIN
Area := (X2 - X1) * (Y2 - Y1);
END;
for which the compiler generates the following code:
BEGIN
3F4B:0037 55 PUSH BP ;Standard Entry Code
3F4B:0038 89E5 MOV BP,SP
3F4B:003A B80200 MOV AX,0002
3F4B:003D 9A4402573F CALL 3F57:0244
3F4B:0042 83EC02 SUB SP,+02
----
Area := (X2 - X1) * (Y2 - Y1);
3F4B:0045 C47E06 LES DI,[BP+06] ;Les Di,Self
3F4B:0048 26 ES:
3F4B:0049 8B4506 MOV AX,[DI+06] :Mov Ax,Es:[Di].Y2
3F4B:004C C47E06 LES DI,[BP+06] ;Les Di,Self
3F4B:004F 26 ES:
3F4B:0050 2B4502 SUB AX,[DI+02] ;Sub Ax,Es:[Di].Y1
3F4B:0053 8BD0 MOV DX,AX
3F4B:0055 C47E06 LES DI,[BP+06] ;Les Di,Self
3F4B:0058 26 ES:
3F4B:0059 8B4504 MOV AX,[DI+04] ;Mov Bx,Es:[Di].X2
3F4B:005C C47E06 LES DI,[BP+06] ;Les Di,Self
3F4B:005F 26 ES:
3F4B:0060 2B05 SUB AX,[DI] ;Sub Bx,Es:[Di].X1
3F4B:0062 F7EA IMUL DX ;IMul Dx
3F4B:0064 8946FE MOV [BP-02],AX ;Mov Area,Ax
----
END;
3F4B:0067 8B46FE MOV AX,[BP-02] ;Standard Exit Code
3F4B:006A 89EC MOV SP,BP
3F4B:006C 5D POP BP
3F4B:006D CA0400 RETF 0004
----
1.1 Using an Assemble statement
The following example illustrates how the "Area" method would be
coded using an Assemble statement. Note that the fields of Rect
are accessible by name without any explicit assembly definition
(e.g. STRUC), and that since Es:Di is never altered, it is not
necessary to reload it before each data access. Standard entry
and exit code is provided by the compiler.
Function Rect.Area: Integer;
BEGIN {- Compiler generates standard entry code -}
Assemble
Les Di,Self ;Load Self Pointer
Mov Ax,Es:[Di].Y2
Sub Ax,Es:[Di].Y1 ;Compute Y2 - Y1
Mov Dx,Es:[Di].X2
Sub Dx,Es:[Di].X1 ;Compute X2 - X1
IMul Dx ;Compute product
Mov Area,Ax ;put in function result
End; {Assemble}
END; {- Compiler generates standard exit code -}
1.2 Using an Internal statement - External Assembly format
The following example illustrates how the "Area" method would be
coded using an Internal statement in the full External Assembly
format. Note that the fields of Rect are again accessible by
name without any explicit assembly definition. As with External
assembly code, entry and exit code must be explicitly provided.
Internal RectMethods
CODE SEGMENT BYTE PUBLIC
ASSUME Cs:CODE
PUBLIC Rect@Area
Rect@Area PROC FAR
@Self EQU DWORD PTR [bp+6]
Push Bp ;Explicit
Mov Bp,Sp ; Entry Code
Les Di,@Self ;Load Self Pointer
Mov Ax,Es:[Di].Y2
Sub Ax,Es:[Di].Y1 ;Compute Y2 - Y1
Mov Dx,Es:[Di].X2
Sub Dx,Es:[Di].X1 ;Compute X2 - X1
IMul Dx ;Compute product
Mov Sp,Bp ;Explicit (Function
Pop Bp ; Exit Result
Retf 4 ; Code is in Ax)
Rect@Area ENDP
CODE ENDS
END
1.3 Using an Internal statement - reduced overhead format
TP&Asm ignores certain "overhead" statements including "Assume"
and "Public", and can make its own determination of the proper PROC
model to use. The following example illustrates how the "Area"
method would be coded using an Internal statement with the minimum
necessary "overhead" statements. Note that the more readable form
"Rect.Area" is permitted in place of "Rect@Area".
Internal RectMethods
CODE SEGMENT
Rect.Area PROC
@Self EQU D[bp+6]
Push Bp ;Explicit
Mov Bp,Sp ; Entry Code
Les Di,@Self ;Load Self Pointer
Mov Ax,Es:[Di].Y2
Sub Ax,Es:[Di].Y1 ;Compute Y2 - Y1
Mov Dx,Es:[Di].X2
Sub